﻿component
{
	include "../../inc/lib.cfm";
	//列表操作
	public any function list(any rc)
	{
		var result = {};
		//从对象中进行Hql模式查询
		var offset = (rc.page-1)*rc.rows;
		hql = "from logistics where " & rc.sqlWhere & rc.sqlSort;
		var result["rows"] = ormExecuteQuery(hql, false, {maxResults=rc.rows, offset=offset});
		var totalhql = "select count(id) as total from logistics where " & rc.sqlWhere ;
		var result["total"] = ormExecuteQuery(totalhql, true);
		for(var i=1; i<=arraylen(result.rows);i++){
			//如果获取area信息，则会递归出category信息内容，速度性能很有影响，所以需要将该属性删除设置null值
			result.rows[i].setlogisticsarea(javacast('null',''));
		}
		return result;
	}

	//删除操作
	public any function delete(numeric id){
		transaction {
            var logistics = entityLoadByPk("logistics", arguments.id);
           	entityDelete(logistics);
            transactionCommit();
        }
	}

	//获取操作
	public any function get(numeric id){
		result = entityLoadByPk("logistics", arguments.id);
		//如果获取area信息，则会递归出category信息内容，速度性能很有影响，所以需要将该属性删除设置null值

		result['expresstypeid']=result.getexpresstype().getid();
		result['paytypeid']=result.getpaytype().getid();

		result.setlogisticsarea(javacast('null',''));
//		result.setExpresstype(javacast('null',''));
//		result.setPaytype(javacast('null',''));
		return result;
	}

	//新增操作
	public any function save(any rc){
		transaction {
            var q = entityNew("logistics");

            for(var i=1;i<= listlen(area); i++){
				var qa = entityNew("logisticsarea");
				var c = entityLoadByPk("category", listgetat(area,i));
				qa.setCategory( c );
				q.addLogisticsarea(qa);
				entitySave(qa);
            }

			var et = entityLoadByPk("category", rc.expresstypeid);
			q.setexpresstype(et);
			var pt = entityLoadByPk("category", rc.paytypeid);
			q.setpaytype(pt);
            //表达式结果内容
			rc.resultexpress = rc.money1 & ',' & rc.money2;
			include "../../inc/saveandupdate.cfm";

        }
        return q;
	}

	//修改操作
	public any function update(any rc){
		transaction {
            var q = entityLoadByPk("logistics", rc.id);
			hql = "delete from logisticsarea where logisticsid = #rc.id#";
			ormExecuteQuery(hql, false);

			for(var i=1;i<= listlen(area); i++){
				var qa = entityNew("logisticsarea");
				var c = entityLoadByPk("category", listgetat(area,i));
				qa.setCategory(c);
				q.addLogisticsarea(qa);
				entitySave(qa);
            }

            var et = entityLoadByPk("category", rc.expresstypeid);
			q.setexpresstype(et);
			var pt = entityLoadByPk("category", rc.paytypeid);
			q.setpaytype(pt);

            //表达式结果内容
			rc.resultexpress = rc.money1 & ',' & rc.money2;
			include "../../inc/saveandupdate.cfm";
        }
        return q;
	}


}